{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "following-convergence", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 12]\n", "[6, 7]\n" ] } ], "source": [ "# Merging Lists\n", "\n", "# Lists are created using square brackets.\n", "L1 = [1, 2, 5, 12]\n", "L2 = [6, 7]\n", "print(L1)\n", "print(L2)" ] }, { "cell_type": "code", "execution_count": 2, "id": "russian-champion", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 5, 12, 6, 7]\n" ] } ], "source": [ "# You can merge the two lists using the addition notation.\n", "print(L1 + L2)" ] }, { "cell_type": "code", "execution_count": 3, "id": "italic-allergy", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 1 2 5 12 6 7]\n" ] } ], "source": [ "# If you're using arrays made from the 'NumPy' module, merging, or concatenating\n", "# two arrays is a little different.\n", "import numpy as np \n", "A1 = np.array(L1)\n", "A2 = np.array(L2)\n", "A3 = np.concatenate((A1, A2))\n", "print(A3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "living-venezuela", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [-5 -6 -7 -8]]\n" ] } ], "source": [ "# You can also build matrices from arrays (assuming the arrays have the same length)\n", "A1 = np.array([1, 2, 3, 4])\n", "A2 = np.array([-5, -6, -7, -8])\n", "M1 = np.matrix([A1, A2])\n", "print(M1)" ] }, { "cell_type": "code", "execution_count": 5, "id": "parliamentary-imagination", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 -5]\n", " [ 2 -6]\n", " [ 3 -7]\n", " [ 4 -8]]\n" ] } ], "source": [ "# The matrix can then be transposed:\n", "M2 = np.transpose(M1)\n", "print(M2)" ] }, { "cell_type": "code", "execution_count": 6, "id": "bibliographic-conference", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.0,\n", " 1.0,\n", " 5.656854249492381,\n", " 15.588457268119896,\n", " 32.0,\n", " 55.90169943749474,\n", " 88.18163074019441,\n", " 129.64181424216494,\n", " 181.01933598375618]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Here's a way that a list can be buily using a loop.\n", "\n", "# First, define an empty list.\n", "listA = []\n", "\n", "# Next add to the list in successive iterations of a loop\n", "for i in range(9):\n", " listA = listA + [i**2.5]\n", "\n", "# Now display the contents of the list. \n", "listA" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }